package cn.sfa.controller.manage;

import java.awt.Rectangle;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jofc2.model.Chart;
import jofc2.model.axis.Label;
import jofc2.model.axis.XAxis;
import jofc2.model.axis.YAxis;
import jofc2.model.elements.LineChart;

import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.sfa.common.pagination.Pagination;
import cn.sfa.common.utils.CommonUtil;
import cn.sfa.common.utils.ImageHepler;
import cn.sfa.common.utils.WebInitBinderContoller;
import cn.sfa.entity.affixItem.SaleAffixItem;
import cn.sfa.entity.routine.SaleAgentbroker;
import cn.sfa.entity.users.SaleUsersLogin;
import cn.sfa.service.affix.AffixService;
import cn.sfa.service.login.LoginService;

@Controller
@RequestMapping("manage/loginlogs/index")
public class ManageLoginLogsController extends WebInitBinderContoller {

	Logger log = LoggerFactory.getLogger(ManageLoginLogsController.class);
	@Autowired
	LoginService loginService;

	// 判断是否是管理员
	public boolean isAdmin(HttpServletRequest request) {
		SaleUsersLogin saleUser = (SaleUsersLogin) request.getSession().getAttribute("user");
		if (saleUser.getUserType().equals("USERTYPE_3"))
			return true;
		return false;
	}

	/**
	 * 获取所有登录日志
	 * 
	 * @author: LiXiang
	 * @param request
	 * @param response
	 * @param map
	 * @return
	 */
	@RequestMapping("/getlogs")
	public String getLogs(
			@RequestParam(value = "fromDate", required = false) String fromDate,
			@RequestParam(value = "toDate", required = false) String toDate,
			HttpServletRequest request, HttpServletResponse response, ModelMap map) {
		int currentPage = 1;
		
		if (!CommonUtil.isEmpty(request.getParameter("currentPage"))) {
			currentPage = Integer.parseInt(request.getParameter("currentPage"));
		}
		String url = request.getContextPath() + "/manage/loginlogs/index/getlogs?fromDate="+fromDate+"&toDate="+toDate;
		Pagination pagination = loginService.getLogsByDate(currentPage, 10, url, fromDate, toDate);
		
		map.put("pagination", pagination);
		map.put("fromDate", fromDate);
		map.put("toDate", toDate);
		return "manage/loginlogs/getlogs";
	}
	
	/**
	 * 获取日志详细
	 * 
	 * @author: LiXiang
	 * @param request
	 * @param response
	 * @param map
	 * @return
	 */
	@RequestMapping("/getlogsdetail")
	public String getLogsDetail(
			@RequestParam(value = "fromDate", required = false) String fromDate,
			HttpServletRequest request, HttpServletResponse response, ModelMap map) {
		int currentPage = 1;
		
		if (!CommonUtil.isEmpty(request.getParameter("currentPage"))) {
			currentPage = Integer.parseInt(request.getParameter("currentPage"));
		}
		fromDate = fromDate.replaceAll("-", "");
		String url = request.getContextPath() + "/manage/loginlogs/index/getlogsdetail?fromDate="+fromDate;
		Pagination pagination = loginService.getLogsDetail(currentPage, 10, url, fromDate);
		
		map.put("pagination", pagination);
		map.put("fromDate", fromDate);
		return "manage/loginlogs/getlogsdetail";
	}
	
	/**
	 * 
	 * 
	 * @author: LiXiang
	 * @param request
	 * @param response
	 * @param map
	 * @return
	 */
	@RequestMapping("/viewchart")
	public String viewChart(
			@RequestParam(value = "fromDate", required = false) String fromDate,
			@RequestParam(value = "toDate", required = false) String toDate,
			HttpServletRequest request, HttpServletResponse response, ModelMap map) {
		map.put("fromDate", fromDate);
		map.put("toDate", toDate);
		return "manage/loginlogs/viewchart";
	}
	
	@RequestMapping("/showchart")
	public String showChart(
			HttpServletRequest request, HttpServletResponse response) throws Exception{  
		String fromDate = request.getParameter("fromDate");
		String toDate = request.getParameter("toDate");
        //y轴数据集合-使用数量  
        List<Number> dataSet = new ArrayList<Number>();  
        //x轴数据集合-浏览器类型  
        List<Label> xLabel = new ArrayList<Label>();  
        //获取需要显示的数据集  
        List browserList = loginService.getLogsByDateForChart(fromDate, toDate);
        for (int i = 0; i < browserList.size(); i++) {  
            Map map = (Map) browserList.get(i);  
            //填充x轴  
            dataSet.add(Integer.parseInt(map.get("TOTAL").toString()));
            //填充y轴  
            xLabel.add(new Label(map.get("dateTime").toString().substring(5)));
        }  
        //设置X轴内容  
        XAxis labels = new XAxis();  
        labels.addLabels(xLabel);  
        //设置Y轴显示值域:Range的三个参数含义为：坐标最小值，最大值和步进值  
        YAxis range = new YAxis();  
        range.setRange(0, 800, 10); 
        //OFC折线图设置  
        LineChart lineChart = new LineChart(LineChart.Style.NORMAL);  
        lineChart.addValues(dataSet);  
        lineChart.setColour("#6666FF");  
        lineChart.setText("使用者数量");  
        lineChart.setWidth(2);
        //图表设置  
        Chart chart = new Chart("趋势图");  
        chart.setXAxis(labels);  
        chart.setYAxis(range);  
        chart.addElements(lineChart);  
        //打印JSON格式的文本  
        response.setContentType("application/json-rpc;charset=utf-8");  
        response.setHeader("Cache-Control", "no-cache");  
        response.setHeader("Expires", "0");  
        response.setHeader("Pragma", "No-cache");  
        response.getWriter().write(chart.toString());  
        return null;  
    }  
}
